package com.kstech.xz3604.ui.customView

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * author: blankii
 * created on: 2025/4/11 15:08
 * description:
 */
class MyProgressBarView : View {
    private val TAG = "MyProgressBar"
    private var mPaint_progress: Paint? = null
    private var mPaint_white: Paint? = null
    private var mViewWidth = 600
    private var mViewHeight = 150

    private var mProgressWidth = 23f //当前进度最大100

    constructor(mContext: Context) : this(mContext, null)
    constructor(mContext: Context, arrrs: AttributeSet?) : this(mContext, arrrs!!, 0)
    constructor(mContext: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        mContext, attrs, defStyleAttr
    ) {
        init(mContext, attrs)
    }

    private fun init(mContext: Context, attrs: AttributeSet) {
        mPaint_progress = Paint()
        mPaint_progress!!.setColor(Color.rgb(67, 114, 19))
        mPaint_progress!!.strokeWidth = 10f
        mPaint_progress!!.style = Paint.Style.FILL
        mPaint_progress!!.isAntiAlias = true
        mPaint_progress!!.textSize = 18f


        mPaint_white = Paint()
        mPaint_white!!.setColor(Color.WHITE)
        mPaint_white!!.strokeWidth = 3f
        mPaint_white!!.style = Paint.Style.STROKE
        mPaint_white!!.isAntiAlias = true

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mViewWidth, mViewHeight)
    }

    /**
     * progress in 0..100
     */
    fun update(progress: Int) {
        if (progress !in 0..100) {
            return
        }
        /**
         * todo 波动范围小的话可以不用绘画
         */
        mProgressWidth = 23f + (mViewWidth - 71f - 23f) * progress / 100f
        postInvalidate()
    }

    override fun onDraw(canvas: Canvas) {
        //背景线条1
        val path = Path()
        path.moveTo(0f, 0f)
        path.lineTo(mViewWidth - 50f, 0f)
        path.lineTo(mViewWidth - 50f, 55f)
        path.lineTo(mViewWidth - 25f, 55f)
        path.lineTo(mViewWidth - 25f, 95f)
        path.lineTo(mViewWidth - 50f, 95f)
        path.lineTo(mViewWidth - 50f, 150f)
        path.lineTo(0f, 150f)
        path.lineTo(0f, 0f)
        canvas.drawPath(path, mPaint_white!!)

        //背景2
        val rectF = RectF(22f, 22f, mViewWidth - 70f, mViewHeight - 22f)
        canvas.drawRect(rectF, mPaint_white!!)

        //进度条
        val progressRect = RectF(23f, 23f, mProgressWidth, mViewHeight - 23f)
        canvas.drawRect(progressRect, mPaint_progress!!);

        super.onDraw(canvas)
    }
}